package com.shop.controller.admin;

import java.sql.SQLException;

import com.jf.core.BaseController;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.shop.bean.ProductBean;
import com.shop.bean.UserInfoBean;
import com.shop.utils.StringUtils;

public class UserController extends BaseController{
	/**
	 * 获取所有的用户信息
	 * */
	public void getAllUser(){
		String select = "select tblyh.*,tblyhxx.*";
		String sqlExceptSelect = "from tblyonghu tblyh inner join tblyhxinxi tblyhxx on tblyh.ucode=tblyhxx.ucode and tblyh.ujuese=0";
		Integer page = getParaToInt("page");
		if(page == null || "".equals(page)){
			page= 1;
		}
		Page<Record> users = Db.paginate(page, 55, select,sqlExceptSelect);
		setAttr("users", users);
		render(WEB_INF_URL+"admin/files/listusers.jsp");
	}
	
	/**
	 * 禁用所有选择的用户
	 * */
	public void delUserList(){
		final String ucodeList = getPara("ucodeList");
		boolean bool = Db.tx(new IAtom(){
			@Override
			public boolean run() throws SQLException {
				boolean result = true;
				String sql_ = "";
				String[] ucodeArr = ucodeList.split("@");
				for(String ucode: ucodeArr){
					sql_ = StringUtils.getSql("sql.find.findUserInfoByCode", ucode);
					result = UserInfoBean.userInfoDao.findFirst(sql_).set("ustate", 0).update();
					if(!result){
						break;
					}
				}
				return result;
			}
		});
		if(bool){
			renderJson("message","true");
		}else{
			renderJson("message","false");
		}
	}
	
	/**
	 * 激活所有选择的用户
	 * */
	public void jhUserList(){
		final String ucodeList = getPara("ucodeList");
		boolean bool = Db.tx(new IAtom(){
			@Override
			public boolean run() throws SQLException {
				boolean result = true;
				String sql_ = "";
				String[] ucodeArr = ucodeList.split("@");
				for(String ucode: ucodeArr){
					sql_ = StringUtils.getSql("sql.find.findUserInfoByCode", ucode);
					result = UserInfoBean.userInfoDao.findFirst(sql_).set("ustate", 1).update();
					if(!result){
						break;
					}
				}
				return result;
			}
		});
		if(bool){
			renderJson("message","true");
		}else{
			renderJson("message","false");
		}
	}
	
	/**
	 * 进入用户详细信息页面
	 * */
	public void goToDetail(){
	    String ucode = getPara("ucode");
	    String sql_ = StringUtils.getSql("sql.find.findUserInfoByCode", ucode);
	    Record record = Db.findFirst(sql_);
	    setAttr("user",record);
	    render(WEB_INF_URL+"admin/files/userdetail.jsp");
	}
}
